*This file produces the results for Tables 4 and 5, which are our main specification.

********************************************************************************
*DEFINE DIRECTORIES
local home CHILD
local main CHILD/JPE
local logs CHILD/JPE/logs
local data CHILD/JPE/data
local results CHILD/JPE/results
local network NETWORK
********************************************************************************

*generate log file
cap log close
cd "`logs'"
local time = subinstr(c(current_date)," ","",.)

*bring in data
cd "`main'"
use regression_data_final_jpe.dta, clear

*merge in single treatment variable
preserve
use all_summary_statistics.dta, clear
keep patientid treated-badfda nd adhd
tempfile temp
save `temp',replace
restore

drop treat
drop drug
drop bad

merge 1:1 patientid using `temp'
keep if _merge==3
drop _merge

*get rid of extra variables
drop tca benzo badfda

*define drug treatment outcome
generate drugs = drug_only+both>0
drop drug_only therapy_only both

*merge in new bad prescribing variables
merge 1:1 patientid using bad_prescribing2.dta
foreach var of varlist fda_ok-red_flag {
	replace `var' = 0 if `var'==.
}
drop _merge

*reorder variables
order treated drugs red_flag grey_area fda_ok, after(patientid)

**incorporate ND diagnosis
drop adhd nd
cd "`home'"
merge 1:1 patientid using neurodevelopmental.dta
keep if _merge==3
drop _merge

*add hurting yourself outcome
cd "`home'"
merge 1:1 patientid using hurting_yourself.dta

*recode kids who don't have hurting yourself records; and hard code to 1's if more than 1
foreach var of varlist before-second_year {
	replace `var' = 0 if `var'==.
	replace `var' = 1 if `var'>1
}
drop _merge

*merge in REVISED DIAGNOSIS INFO
cd "`home'"
merge 1:1 patientid using diagnosis_revised_final.dta
keep if _merge==3
drop _merge

*save final sample
preserve
cd "`data'"
keep patientid zcta_kid zip3 year treated drugs red_flag grey_area fda_ok
save final_jpe_sample.dta, replace
restore

*merge in additional instrument
destring zcta_kid, replace
cd "`home'"
merge m:1 zcta_kid year using market-measures-antipsych.dta
drop if _merge==2
drop _merge

*look for which leave out measure needed
cd "`data'"
merge 1:1 patientid using status_of_leave_out.dta
drop _merge

generate share_ap = share_antipsych
replace share_ap = share_anti_yes if antipsychotic==1
replace share_ap = share_anti_no if antipsychotic==0

*define variables (for lasso/tsls)
local instruments share_mh share_psych share_gp s1_by_psych share_ap 
local severity first_hospital first_er first_eval in_hospital_pre
local other female month year

*diagnosis vectors
local diagnosis_int depression-any_multiple only_depression-formal_multiple nd
local diagnosis_control depression anxiety adjustment_anxiety adjustment_depression adjustment_dax  nd

**********************************************************************
**********************************************************************
**********************************************************************
*Run LASSO
**********************************************************************
**********************************************************************
**********************************************************************

preserve
****************************************
*run first stage regressions (among all kids)
****************************************

*effect of treatment
local var treat
quietly rlasso `var' (i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), partial((i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)) cluster(zip3)  maxpsiiter(10) lalt supscore 
local ins_all_treat `e(selected)'
macro list _ins_all_treat

****************************************
*run first-stage regressions for branch 1 (among treated)
keep if treated
****************************************

*effect of drug treatment
local var drugs
quietly rlasso `var' (i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), partial((i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)) cluster(zip3)  maxpsiiter(10) lalt supscore 
local ins_treat_drug `e(selected)'
macro list _ins_treat_drug

************************************************
*run first-stage regressions for branch 2 (among drug treated)
keep if drugs
************************************************

*effect of bad drug treatment
local var red_flag
quietly rlasso `var' (i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3) i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), partial((i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)) cluster(zip3)  
local ins_drug_rf `e(selected)'
macro list _ins_drug_rf

*effect of grey area drug treatment
local var grey_area
quietly rlasso `var' (i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3) i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), partial((i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)) cluster(zip3) 
local ins_drug_ga `e(selected)'
macro list _ins_drug_ga

*effect of fda-ok treatment
local var fda_ok
quietly rlasso `var' (i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)  i.age#c.(`instruments') i1.(`severity' `diagnosis_int' female)#c.(`instruments') c.(`instruments')##c.(`instruments'), partial((i.(`other') i.age i.(`severity') i.(`diagnosis_control') i.zip3)) cluster(zip3)  
local ins_drug_fda `e(selected)'
macro list _ins_drug_fda

restore

**********************************************************************
**********************************************************************
**********************************************************************
*Now, run analysis
**********************************************************************
**********************************************************************
**********************************************************************
		
*generate log file
cd "`results'"
cap log close
local time = subinstr(c(current_date)," ","",.)
log using main_results_FINAL_`time'.txt, text replace

*list the instruments
macro list _ins_all_treat _ins_treat_drug _ins_drug_rf _ins_drug_ga _ins_drug_fda

****************************************
*run first stage regressions (among all kids)
****************************************
local var treated
local instrument `ins_all_treat'

*generate above/below median cost variable
foreach x in tot3m tot12m tot24m {
	quietly summarize `x'_orig,d
	generate above_`x' = `x'_orig>=r(p50)
}

*define outcomes
local outcomes tot3m tot12m tot24m above_tot3m above_tot12m above_tot24m hosp3m hosp12m hosp24m first_three first_year second_year 

eststo clear
foreach outcome of local outcomes {
eststo: quietly areg `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') `var',  cluster(zip3) absorb(zip3)
}
label variable `var' "OLS"
estimates store ols_all_treat

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3)  stats(r2, fmt(%9.3f)) mtitles("3-Months" "1-Year" "2-Years" "3-Months" "1-Year" "2-Years" "3-Months" "1-Year" "2-Years" "3-Months" "1-Year" "2-Years" ) mgroups("Log Total Costs" "DV: Above Median Total Costs?" "DV: Visited facility?" "DV: Any Harm Behavior?" , pattern(1 0 0 1 0 0 1 0 0 1 0 0) span) nonumber title("Panel A: Effect of Treatment") noabbrev star(+ 0.10 * 0.05 ** 0.01) label replace noobs nonotes nogap

eststo clear
foreach outcome of local outcomes {
eststo: quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`instrument') i.zip3 ,  cluster(zip3) partial(i.zip3) 
quietly summarize `outcome' if e(sample)
estadd scalar Mean = r(mean)
}
label variable `var' "All LASSO"
estimates store lasso_all_treat

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) star(+ 0.10 * 0.05 ** 0.01) nonumber nomtitle nonotes stats(N Mean, fmt(%9.0f %9.3f)) noobs label append nogap

****************************************
*run first-stage regressions for branch 1 (among treated)
keep if treated
****************************************

*effect of drug treatment
local var drugs
local instrument `ins_treat_drug'

*redefine above/below median cost variable
foreach x in tot3m tot12m tot24m {
	quietly summarize `x'_orig,d
	replace above_`x' = `x'_orig>=r(p50)
}

*define outcomes
local outcomes tot3m tot12m tot24m above_tot3m above_tot12m above_tot24m hosp3m hosp12m hosp24m first_three first_year second_year 

eststo clear
foreach outcome of local outcomes {
eststo: quietly areg `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') `var',  cluster(zip3) absorb(zip3)
}
label variable `var' "OLS"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) stats(r2, fmt(%9.3f)) nonumber title("Panel B: Effect of Drug Treatment")  append noobs nonotes nogap label nomtitle star(+ 0.10 * 0.05 ** 0.01)

eststo clear
foreach outcome of local outcomes {
eststo: quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`instrument') i.zip3 ,  cluster(zip3) partial(i.zip3) 
quietly summarize `outcome' if e(sample)
estadd scalar Mean = r(mean)
}
label variable `var' "All LASSO"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) nonumber noabbrev star(+ 0.10 * 0.05 ** 0.01) label append nogap nomtitle stats(N Mean, fmt(%9.0f %9.3f)) nonotes

************************************************
*run first-stage regressions for branch 2 (among drug treated)
keep if drugs
************************************************

*redefine above/below median cost variable
foreach var in tot3m tot12m tot24m {
	quietly summarize `var'_orig,d
	replace above_`var' = `var'_orig>=r(p50)
}

*define outcomes
local outcomes tot3m tot12m tot24m above_tot3m above_tot12m above_tot24m hosp3m hosp12m hosp24m first_three first_year second_year 

*effect of red-flag drug treatment
local var red_flag
local instrument `ins_drug_rf'

eststo clear
foreach outcome of local outcomes {
eststo: quietly areg `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') `var',  cluster(zip3) absorb(zip3)
}
label variable `var' "OLS"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) stats(r2, fmt(%9.3f)) nonumber title("Panel C: Effect of Red-Flag Drug Treatment") append noobs nonotes nogap label nomtitle star(+ 0.10 * 0.05 ** 0.01)

eststo clear
foreach outcome of local outcomes {
eststo: quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`instrument') i.zip3 ,  cluster(zip3) partial(i.zip3) 
}
label variable `var' "All LASSO"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) nonumber noabbrev star(+ 0.10 * 0.05 ** 0.01) label append nogap nomtitle nonotes noobs

*effect of grey-area drug treatment
local var grey_area
local instrument `ins_drug_ga'

eststo clear
foreach outcome of local outcomes {
eststo: quietly areg `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') `var',  cluster(zip3) absorb(zip3)
}
label variable `var' "OLS"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) stats(r2, fmt(%9.3f)) nonumber title("Panel C: Effect of Grey-Area Drug Treatment") append noobs nonotes nogap label nomtitle star(+ 0.10 * 0.05 ** 0.01)

eststo clear
foreach outcome of local outcomes {
eststo: quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`instrument') i.zip3 ,  cluster(zip3) partial(i.zip3) 
}
label variable `var' "All LASSO"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) nonumber noabbrev star(+ 0.10 * 0.05 ** 0.01) label append nogap nomtitle nonotes noobs


*effect of fda-ok drug treatment
local var fda_ok
local instrument `ins_drug_fda'

eststo clear
foreach outcome of local outcomes {
eststo: quietly areg `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') `var',  cluster(zip3) absorb(zip3)
}
label variable `var' "OLS"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) stats(r2, fmt(%9.3f)) nonumber title("Panel C: Effect of FDA-Approved Drug Treatment") append noobs nonotes nogap label nomtitle star(+ 0.10 * 0.05 ** 0.01)

eststo clear
foreach outcome of local outcomes {
eststo: quietly ivreg2 `outcome' i.(`other') i.age i.(`severity') i.(`diagnosis_control') (`var'=`instrument') i.zip3 ,  cluster(zip3) partial(i.zip3) 
quietly summarize `outcome' if e(sample)
estadd scalar Mean = r(mean)
}
label variable `var' "All LASSO"

sleep 100
esttab using main_results_FINAL.csv, keep(`var') b(3) se(3) nonumber noabbrev star(+ 0.10 * 0.05 ** 0.01) label append nogap nomtitle stats(N Mean, fmt(%9.0f %9.3f)) nonotes

cap log close